/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
//caudillo joto
package tarearonald;


import java.sql.*;

import java.util.logging.Level;
import java.util.logging.Logger; 

public class Conex {

    public Connection conectar() {
        Connection con = null;
        try {
			//el oscar es puto
         //jotojoto   
            Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
            String connectionUrl = "jdbc:sqlserver://" + "servermn2.no-ip.org" + ":1400;"
                +                  "databaseName=" + "Tarea" + ";user=" + "sa" 
                                  + "; password=" + "mnavzmgr" + ";";
//            Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
//            String connectionUrl = "jdbc:sqlserver://" + "localhost" + ":1400;"
//                +                  "databaseName=" + "Pruebas" + ";user=" + "sa" 
//                                  + "; password=" + "1234" + ";";
            con = DriverManager.getConnection(connectionUrl);
            
        } catch (Exception ex) {
            Logger.getLogger(Conex.class.getName()).log(Level.SEVERE, null, ex);
        }
        return con;
    }
    
    public void close(Connection con)
    {
        try {
            con.close();
        } catch (SQLException ex) {
            Logger.getLogger(Conex.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

public String[][] select(Connection con, String sql) {
        ResultSet res = null;
        Statement stmt = null;
        String result[][];
        int cont = 1, contador = 0, j = 0, k = 0;
        String auxiliar = "", auxiliar2 = "";
        try {
            stmt = con.createStatement();
            res = stmt.executeQuery(sql);
            cont = res.getMetaData().getColumnCount();
            while (res.next()) {
                for (int i = 1; i <= cont; i++) {
                    auxiliar += res.getString(i) + "ç";

                }
                auxiliar += "^";
                contador++;
            }


        } catch (Exception ex) {
            Logger.getLogger(Conex.class.getName()).log(Level.SEVERE, null, ex);
        }
        
        result = new String[contador][cont];
        
        
        for (int i = 0; i < auxiliar.length(); i++) {
            if (auxiliar.charAt(i) == 'ç') {
                result[k][j] = "" + auxiliar2;
                //System.out.print(""+auxiliar2); 
                auxiliar2 = "";
                j++;
            } else if (auxiliar.charAt(i) == '^') {
                k++;
                j = 0;
            } else {
                auxiliar2 += "" + auxiliar.charAt(i);
            }
        }
        return result;
    }

    public int insertar(Connection con, String sql) {
        int afectados = 0;
        Statement stmt = null;
        try {
            stmt = con.createStatement();
        } catch (SQLException ex) {
            Logger.getLogger(Conex.class.getName()).log(Level.SEVERE, null, ex);
        }
        try {
            afectados = stmt.executeUpdate(sql);
        } catch (SQLException ex) {
            Logger.getLogger(Conex.class.getName()).log(Level.SEVERE, null, ex);
        }
        //System.out.println("" + afectados);
        return afectados;
    }
    
    public int borrar(Connection con, String sql)
    {
        int afectados = 0;
        Statement stmt = null;
        try {
            stmt = con.createStatement();
        } catch (SQLException ex) {
            Logger.getLogger(Conex.class.getName()).log(Level.SEVERE, null, ex);
        }
        try {
            afectados = stmt.executeUpdate(sql);
        } catch (SQLException ex) {
            Logger.getLogger(Conex.class.getName()).log(Level.SEVERE, null, ex);
        }
        System.out.println("" + afectados);
        return afectados;
        
    }
    
    public int modificar(Connection con, String sql)
    {
        int afectados = 0;
        Statement stmt = null;
        try {
            stmt = con.createStatement();
        } catch (SQLException ex) {
            Logger.getLogger(Conex.class.getName()).log(Level.SEVERE, null, ex);
        }
        try {
            afectados = stmt.executeUpdate(sql);
        } catch (SQLException ex) {
            Logger.getLogger(Conex.class.getName()).log(Level.SEVERE, null, ex);
        }
        System.out.println("" + afectados);
        return afectados;
        
    }
}
